package com.neuedu.corejava.ch12;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 
 * @author myh
 * @version 2019年8月14日下午1:42:24
 *
 */
public class TestStudent {
	/**
	 * 保存学生信息的文件
	 */
	private static final String FILE_NAME = "c:\\student.data";

	public static void inputStudentInfo() {
		@SuppressWarnings("resource")
		Scanner scanner = new Scanner(System.in);
		System.out.println("----------------请输入三个学生的信息---------------");
		List<Student> list = new ArrayList<>();
		Student student = null;
		for (int i = 0; i < 3; i++) {
			int id = 0;
			while (true) {
				try {
					System.out.println("请输入学号：");
					String idString = scanner.nextLine();
					id = Integer.parseInt(idString);
					break;
				} catch (NumberFormatException e) {
				}
			}
			System.out.println("请输入姓名：");
			String nameString = scanner.nextLine();
			int age = 0;
			while (true) {
				try {
					System.out.println("请输入年龄：");
					String ageString = scanner.nextLine();
					age = Integer.parseInt(ageString);
					break;
				} catch (NumberFormatException e) {
				}

			}

			System.out.println("请输入性别：");
			String sexString = scanner.nextLine();
			student = new Student(id, nameString, age, sexString);
			list.add(student);
		}

		File file = new File(FILE_NAME);
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		try {
			if (!file.exists()) {
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
			// 节点流 -- 操作文件
			fos = new FileOutputStream(file);
			// 处理流 -- 对流进行包装
			oos = new ObjectOutputStream(fos);
			// 使用对象流吧一个java.lang.Object类的对象输出到一个流（网络流，管道流，文件流）
			oos.writeObject(list);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
				}
			}
		}

	}

	public static void readDataFromFile() {
		File file = new File(FILE_NAME);
		if (!file.exists()) {
			System.out.println("找不到文件:" + FILE_NAME);
			return;
		}
		FileInputStream fis = null;
		ObjectInputStream ois = null;

		try {
			fis = new FileInputStream(file);
			ois = new ObjectInputStream(fis);
			Object object = ois.readObject();
			List<Student> list = (List<Student>) object;

			for (Student student : list) {
				System.out.println(student);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
				}
			}
		}
	}

	public static void main(String[] args) {
		// 编写一段代码，从键盘输入三个学生信息，并保存到文件中
		inputStudentInfo();
		// 编写一段代码，从文件中读取数据，并还原为一组学生信息
		readDataFromFile();
	}

}
